{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Negative Prompting and Avoiding Undesired Outputs\n",
    "\n",
    "## Overview\n",
    "This tutorial explores the concept of negative prompting and techniques for avoiding undesired outputs when working with large language models. We'll focus on using OpenAI's GPT models and the LangChain library to implement these strategies.\n",
    "\n",
    "## Motivation\n",
    "As AI language models become more powerful, it's crucial to guide their outputs effectively. Negative prompting allows us to specify what we don't want in the model's responses, helping to refine and control the generated content. This approach is particularly useful when dealing with sensitive topics, ensuring factual accuracy, or maintaining a specific tone or style in the output.\n",
    "\n",
    "## Key Components\n",
    "1. Using negative examples to guide the model\n",
    "2. Specifying exclusions in prompts\n",
    "3. Implementing constraints using LangChain\n",
    "4. Evaluating and refining negative prompts\n",
    "\n",
    "## Method Details\n",
    "We'll start by setting up our environment with the necessary libraries. Then, we'll explore different techniques for negative prompting:\n",
    "\n",
    "1. Basic negative examples: We'll demonstrate how to provide examples of undesired outputs to guide the model.\n",
    "2. Explicit exclusions: We'll use prompts that specifically state what should not be included in the response.\n",
    "3. Constraint implementation: Using LangChain, we'll create more complex prompts that enforce specific constraints on the output.\n",
    "4. Evaluation and refinement: We'll discuss methods to assess the effectiveness of our negative prompts and iteratively improve them.\n",
    "\n",
    "Throughout the tutorial, we'll use practical examples to illustrate these concepts and provide code snippets for implementation.\n",
    "\n",
    "## Conclusion\n",
    "By the end of this tutorial, you'll have a solid understanding of negative prompting techniques and how to apply them to avoid undesired outputs from language models. These skills will enable you to create more controlled, accurate, and appropriate AI-generated content for various applications."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "First, let's import the necessary libraries and set up our environment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.prompts import PromptTemplate\n",
    "\n",
    "# Load environment variables\n",
    "from dotenv import load_dotenv\n",
    "load_dotenv()\n",
    "\n",
    "# Set up OpenAI API key\n",
    "os.environ[\"OPENAI_API_KEY\"] = os.getenv('OPENAI_API_KEY')\n",
    "\n",
    "# Initialize the language model\n",
    "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n",
    "\n",
    "def get_response(prompt):\n",
    "    \"\"\"Helper function to get response from the language model.\"\"\"\n",
    "    return llm.invoke(prompt).content"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Using Negative Examples\n",
    "\n",
    "Let's start with a simple example of using negative examples to guide the model's output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Photosynthesis is the process by which green plants, algae, and some bacteria make their own food using sunlight. They take in carbon dioxide from the air and water from the soil. Using sunlight, they convert these ingredients into glucose, a type of sugar, which they use for energy and growth. During this process, they also release oxygen into the air, which is essential for many living beings.\n"
     ]
    }
   ],
   "source": [
    "negative_example_prompt = PromptTemplate(\n",
    "    input_variables=[\"topic\"],\n",
    "    template=\"\"\"Provide a brief explanation of {topic}. \n",
    "    Do NOT include any of the following in your explanation:\n",
    "    - Technical jargon or complex terminology\n",
    "    - Historical background or dates\n",
    "    - Comparisons to other related topics\n",
    "    Your explanation should be simple, direct, and focus only on the core concept.\"\"\"\n",
    ")\n",
    "\n",
    "response = get_response(negative_example_prompt.format(topic=\"photosynthesis\"))\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Specifying Exclusions\n",
    "\n",
    "Now, let's explore how to explicitly specify what should be excluded from the response."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Exercise offers a multitude of benefits that extend beyond physical appearance. Engaging in regular physical activity enhances mental well-being by reducing symptoms of anxiety and depression, and promoting a sense of accomplishment and improved mood through the release of endorphins. It also boosts cognitive function, improving memory and concentration, which can lead to greater productivity in daily tasks. Additionally, exercise strengthens the cardiovascular system, increases stamina, and supports overall health by enhancing immune function and reducing the risk of chronic diseases. Ultimately, incorporating exercise into one’s routine fosters a greater sense of vitality and resilience in daily life.\n"
     ]
    }
   ],
   "source": [
    "exclusion_prompt = PromptTemplate(\n",
    "    input_variables=[\"topic\", \"exclude\"],\n",
    "    template=\"\"\"Write a short paragraph about {topic}. \n",
    "    Important: Do not mention or reference anything related to {exclude}.\"\"\"\n",
    ")\n",
    "\n",
    "response = get_response(exclusion_prompt.format(\n",
    "    topic=\"the benefits of exercise\",\n",
    "    exclude=\"weight loss or body image\"\n",
    "))\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Implementing Constraints\n",
    "\n",
    "Let's use LangChain to create more complex prompts that enforce specific constraints on the output."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Artificial intelligence refers to the development of algorithms and computational models that enable machines to perform tasks typically requiring cognitive functions. These tasks include data analysis, pattern recognition, natural language processing, and decision-making. AI systems utilize techniques such as machine learning, deep learning, and neural networks to process large datasets, learn from experience, and improve performance over time. Applications span various fields, including healthcare, finance, and autonomous systems, enhancing efficiency and enabling complex problem-solving capabilities. AI's effectiveness is dependent on data quality, computational resources, and the design of the underlying models.\n"
     ]
    }
   ],
   "source": [
    "constraint_prompt = PromptTemplate(\n",
    "    input_variables=[\"topic\", \"style\", \"excluded_words\"],\n",
    "    template=\"\"\"Write a {style} description of {topic}.\n",
    "    Constraints:\n",
    "    1. Do not use any of these words: {excluded_words}\n",
    "    2. Keep the description under 100 words\n",
    "    3. Do not use analogies or metaphors\n",
    "    4. Focus only on factual information\"\"\"\n",
    ")\n",
    "\n",
    "response = get_response(constraint_prompt.format(\n",
    "    topic=\"artificial intelligence\",\n",
    "    style=\"technical\",\n",
    "    excluded_words=\"robot, human-like, science fiction\"\n",
    "))\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Evaluation and Refinement\n",
    "\n",
    "To evaluate and refine our negative prompts, we can create a function that checks if the output adheres to our constraints."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Evaluation results: {'word_count': True, 'no_excluded_words': True, 'no_analogies': False}\n",
      "\n",
      "Refined response:\n",
      " Artificial intelligence (AI) refers to the development of algorithms and systems that enable machines to perform tasks typically requiring cognitive functions. This includes learning from data, recognizing patterns, making decisions, and understanding natural language. AI encompasses various subfields such as machine learning, deep learning, and natural language processing. It relies on computational models and vast datasets to improve performance over time. Applications range from image and speech recognition to autonomous systems and data analysis, impacting industries including healthcare, finance, and transportation. AI systems can operate in real-time and adapt to new information, enhancing efficiency and accuracy.\n",
      "\n",
      "Refined evaluation results: {'word_count': True, 'no_excluded_words': True, 'no_analogies': False}\n"
     ]
    }
   ],
   "source": [
    "def evaluate_output(output, constraints):\n",
    "    \"\"\"Evaluate if the output meets the given constraints.\"\"\"\n",
    "    results = {}\n",
    "    for constraint, check_func in constraints.items():\n",
    "        results[constraint] = check_func(output)\n",
    "    return results\n",
    "\n",
    "# Define some example constraints\n",
    "constraints = {\n",
    "    \"word_count\": lambda x: len(x.split()) <= 100,\n",
    "    \"no_excluded_words\": lambda x: all(word not in x.lower() for word in [\"robot\", \"human-like\", \"science fiction\"]),\n",
    "    \"no_analogies\": lambda x: \"like\" not in x.lower() and \"as\" not in x.lower()\n",
    "}\n",
    "\n",
    "# Evaluate the previous output\n",
    "evaluation_results = evaluate_output(response, constraints)\n",
    "print(\"Evaluation results:\", evaluation_results)\n",
    "\n",
    "# If the output doesn't meet all constraints, we can refine our prompt\n",
    "if not all(evaluation_results.values()):\n",
    "    refined_prompt = constraint_prompt.format(\n",
    "        topic=\"artificial intelligence\",\n",
    "        style=\"technical and concise\",  # Added 'concise' to address word count\n",
    "        excluded_words=\"robot, human-like, science fiction, like, as\"  # Added 'like' and 'as' to avoid analogies\n",
    "    )\n",
    "    refined_response = get_response(refined_prompt)\n",
    "    print(\"\\nRefined response:\\n\", refined_response)\n",
    "    \n",
    "    # Evaluate the refined output\n",
    "    refined_evaluation = evaluate_output(refined_response, constraints)\n",
    "    print(\"\\nRefined evaluation results:\", refined_evaluation)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
